/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.gerenciadorfinanceiro.face;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import org.gerenciadorfinanceiro.ejb.entity.Income;
import org.gerenciadorfinanceiro.ejb.entity.User;
import org.gerenciadorfinanceiro.ejb.session.BusinessViolation;
import org.gerenciadorfinanceiro.ejb.session.interfaces.IncomeSessionLocal;

/**
 *
 * @author eduardo
 */

public class IncomeFace {

    @EJB
    private IncomeSessionLocal incomeBean;

    private List<Income> incomes = null;
    private Income incomeInEdit = null;

    /** Creates a new instance of IncomeFace */
    public IncomeFace() {
    }

    public String doStartAdd() {
        this.setIncomeInEdit(new Income(0));
        return "startAddIncome";
    }

    public String doAdd() throws BusinessViolation {
        System.out.println("DOAdd: "+this.incomeInEdit);
        incomeInEdit.setId(0);
        incomeInEdit.setOwner((User)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("usuarioLogado"));
        System.out.println("DOAdd 2: "+this.incomeInEdit);
        incomeBean.addIncome(incomeInEdit);
        System.out.println("DOAdd 3: "+this.incomeInEdit);
        incomes = null;
        incomeInEdit = null;
        return "finishAddIncome";
    }

    public String doRemove() throws BusinessViolation {
        incomeBean.deleteIncome(incomeInEdit);
        incomes = null;
        return "finishRemoveIncome";
    }
    
    public String doStartUpdate() throws BusinessViolation {
        return "startUpdateIncome";
    }
    
    public String doUpdate() throws BusinessViolation {
        incomeBean.updateIncome(incomeInEdit);
        incomeInEdit = null;
        return "finishUpdateIncome";
    }


    /**
     * @return the incomeBean
     */
    public IncomeSessionLocal getIncomeBean() {
        return incomeBean;
    }

    /**
     * @return the incomes
     */
    public List<Income> getIncomes() {
        if (this.incomes == null) {
            User user = (User)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("usuarioLogado");
            Calendar c = GregorianCalendar.getInstance();
            this.setIncomes(incomeBean.getIncomes(user, c.get(Calendar.MONTH)+1, c.get(Calendar.YEAR)));
        }

        return this.incomes;
    }

    /**
     * @param incomes the incomes to set
     */
    public void setIncomes(List<Income> incomes) {
        this.incomes = incomes;
    }

    /**
     * @return the incomeInEdit
     */
    public Income getIncomeInEdit() {
        System.out.println("GETIncomeInEdit: "+this.toString());
        System.out.println("GETIncomeInEdit: "+this.incomeInEdit);
        return incomeInEdit;
    }

    /**
     * @param incomeInEdit the incomeInEdit to set
     */
    public void setIncomeInEdit(Income incomeInEdit) {
        System.out.println("SETIncomeInEdit: "+this.incomeInEdit);
        this.incomeInEdit = incomeInEdit;
    }

}
